package com.faithbj.common.model.fm;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.faithbj.common.model.BaseEntity;
import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.model.Enclosure;
import com.faithbj.common.utils.CommonUtil;
import com.faithbj.common.utils.DictionaryUtil;
import com.faithbj.common.utils.JsonUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;

/**
 * 家庭成员基本情况表
 * 
 * @author XiaoQuan
 */
@Entity
@Table(name = "family_member")
@JsonIgnoreProperties({ "checkTotal", "enclosureList", "deposites", "stocks", "openFunds", "closeFunds", "insurances",
    "bonds", "futures", "cars", "houses", "idTypeAlias", "enclosures", "educationAlias", "maritalStatusAlias"})
public class FamilyMember extends BaseEntity {

    private static final long serialVersionUID = -1189848055547645033L;
    /**
     * 关联的核对总体情况表ID
     */
    private CheckTotal checkTotal;
    /**
     * 姓名
     */
    private String name;
    /**
     * 性别
     */
    private String gender;
    /**
     * 出生日期
     */
    private Date birthday;
    /**
     * 证件类型
     */
    private String idType;
    /**
     * 证件号码
     */
    private String idNumber;
    /**
     * 民族
     */
    private String nation;
    /**
     * 国籍
     */
    private String country;
    /**
     * 政治面貌
     */
    private String politicalStatus;
    /**
     * 婚姻状况
     */
    private String maritalStatus;
    /**
     * 结婚登记日期
     */
    private Date marriedDate;
    /**
     * 户籍类型
     */
    private String householdType;
    /**
     * 户籍所在地地址
     */
    private String householdAddress;
    /**
     * 户籍所在地行政区划代码
     */
    private String householdDistrictCode;
    /**
     * 入市户籍时间(精确到年月日)
     */
    private Date settleTime;
    /**
     * 入申请区户籍时间(精确到年月日)
     */
    private Date settleCountyTime;

    /**
     * 当前工作单位(全称)
     */
    private String workUnit;

    /**
     * 现居住地地址:regioncode:具体地址
     */
    private String livingAddress;
    /**
     * 现居住地行政区划代码
     */
    private String livingDistrictCode;
    /**
     * 现居住地开始居住时间
     */
    private Date startDateLiving;
    /**
     * 文化程度
     */
    private String education;
    /**
     * 健康状况
     */
    private String healthStatus;
    /**
     * 劳动能力丧失状况
     */
    private String laborLossStatus;
    /**
     * 残疾等级
     */
    private String disabilityDegree;
    /**
     * 所患主要疾病名称
     */
    private String diseaseName;
    /**
     * 是否主申请人
     */
    private Boolean mainApplicant;
    /**
     * 与主申请人关系
     */
    private String relationMainApplicant;
    /**
     * 固定电话
     */
    private String phone;
    /**
     * 手机号码
     */
    private String mobile;
    /**
     * 邮政编码
     */
    private String zipCode;
    /**
     * 通讯地址
     */
    private String contactAddress;
    /**
     * 家庭成员收入
     */
    private FamilyMemberIncome income;
    /**
     * 是否已经填写可支配收入申报
     */
    private Boolean isReportDPI;
    /**
     * 是否已经填写财产申报
     */
    private Boolean isReportProperty;
    /**
     * json格式存放申请材料
     */
    private String enclosure;

    // 可支配收入是否存在差异
    private Boolean incomeDifference;

    // 可支配收入核对意见
    private String incomeDiffDes;

    // 财产状况是否存在差异
    private Boolean propertyDifference;

    // 财产状况差异情况
    private String propertyDiffDes;

    @OneToOne(cascade = { CascadeType.ALL })
    @JoinColumn(name = "income")
    public FamilyMemberIncome getIncome() {
        return income;
    }

    @ManyToOne
    @JoinColumn(name = "check_total", nullable = true)
    public CheckTotal getCheckTotal() {
        return checkTotal;
    }

    @Column(name = "name", length = 30, nullable = false)
    public String getName() {
        return name;
    }

    @Column(name = "gender", nullable = true)
    public String getGender() {
        return gender;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "birthday", nullable = true)
    public Date getBirthday() {
        return birthday;
    }

    @Column(name = "id_type", nullable = false)
    public String getIdType() {
        return idType;
    }

    @Column(name = "id_number", nullable = false, length = 18)
    public String getIdNumber() {
        return idNumber;
    }

    @Column(name = "nation", nullable = true)
    public String getNation() {
        return nation;
    }

    @Column(name = "country", nullable = true)
    public String getCountry() {
        return country;
    }

    @Column(name = "political_status", nullable = true)
    public String getPoliticalStatus() {
        return politicalStatus;
    }

    @Column(name = "marital_status", nullable = true)
    public String getMaritalStatus() {
        return maritalStatus;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "married_date", nullable = true)
    public Date getMarriedDate() {
        return marriedDate;
    }

    @Column(name = "household_type", nullable = true)
    public String getHouseholdType() {
        return householdType;
    }

    @Column(name = "household_address", nullable = true, length = 80)
    public String getHouseholdAddress() {
        return householdAddress;
    }

    @Column(name = "household_district_code", nullable = true)
    public String getHouseholdDistrictCode() {
        return householdDistrictCode;
    }

    @Column(name = "living_address", nullable = true, length = 80)
    public String getLivingAddress() {
        return livingAddress;
    }

    @Column(name = "living_district_code", nullable = true)
    public String getLivingDistrictCode() {
        return livingDistrictCode;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "start_date_living", nullable = true)
    public Date getStartDateLiving() {
        return startDateLiving;
    }

    @Column(name = "education", nullable = true)
    public String getEducation() {
        return education;
    }

    @Column(name = "health_status", nullable = true)
    public String getHealthStatus() {
        return healthStatus;
    }

    @Column(name = "labor_loss_status", nullable = true)
    public String getLaborLossStatus() {
        return laborLossStatus;
    }

    @Column(name = "disability_degree", nullable = true)
    public String getDisabilityDegree() {
        return disabilityDegree;
    }

    @Column(name = "disease_name", nullable = true, length = 20)
    public String getDiseaseName() {
        return diseaseName;
    }

    @Column(name = "main_applicant", nullable = false)
    public Boolean getMainApplicant() {
        return mainApplicant;
    }

    @Column(name = "relation_main_applicant", nullable = false)
    public String getRelationMainApplicant() {
        return relationMainApplicant;
    }

    @Column(name = "phone")
    public String getPhone() {
        return phone;
    }

    @Column(name = "mobile")
    public String getMobile() {
        return mobile;
    }

    @Column(name = "zip_code")
    public String getZipCode() {
        return zipCode;
    }

    @Column(name = "contact_address")
    public String getContactAddress() {
        return contactAddress;
    }

    @Lob
    public String getEnclosure() {
        return enclosure;
    }

    @Column(name = "settle_time")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getSettleTime() {
        return settleTime;
    }

    @Column(name = "settle_county_time")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getSettleCountyTime() {
        return settleCountyTime;
    }

    @Column(name = "work_unit")
    public String getWorkUnit() {
        return workUnit;
    }

    @Column(name = "is_reportdpi")
    public Boolean getIsReportDPI() {
        return isReportDPI;
    }

    @Column(name = "is_report_property")
    public Boolean getIsReportProperty() {
        return isReportProperty;
    }

    @Column(name = "income_difference")
    public Boolean getIncomeDifference() {
        return incomeDifference;
    }

    @Column(name = "income_diff_description")
    public String getIncomeDiffDes() {
        return incomeDiffDes;
    }

    @Column(name = "property_difference")
    public Boolean getPropertyDifference() {
        return propertyDifference;
    }

    @Column(name = "property_diff_description")
    public String getPropertyDiffDes() {
        return propertyDiffDes;
    }

    public void setIncome(FamilyMemberIncome income) {
        this.income = income;
    }

    public void setCheckTotal(CheckTotal checkTotal) {
        this.checkTotal = checkTotal;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public void setPoliticalStatus(String politicalStatus) {
        this.politicalStatus = politicalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public void setMarriedDate(Date marriedDate) {
        this.marriedDate = marriedDate;
    }

    public void setHouseholdType(String householdType) {
        this.householdType = householdType;
    }

    public void setHouseholdAddress(String householdAddress) {
        this.householdAddress = householdAddress;
    }

    public void setHouseholdDistrictCode(String householdDistrictCode) {
        this.householdDistrictCode = householdDistrictCode;
    }

    public void setLivingAddress(String livingAddress) {
        this.livingAddress = livingAddress;
    }

    public void setLivingDistrictCode(String livingDistrictCode) {
        this.livingDistrictCode = livingDistrictCode;
    }

    public void setStartDateLiving(Date startDateLiving) {
        this.startDateLiving = startDateLiving;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public void setHealthStatus(String healthStatus) {
        this.healthStatus = healthStatus;
    }

    public void setLaborLossStatus(String laborLossStatus) {
        this.laborLossStatus = laborLossStatus;
    }

    public void setDisabilityDegree(String disabilityDegree) {
        this.disabilityDegree = disabilityDegree;
    }

    public void setDiseaseName(String diseaseName) {
        this.diseaseName = diseaseName;
    }

    public void setMainApplicant(Boolean mainApplicant) {
        this.mainApplicant = mainApplicant;
    }

    public void setRelationMainApplicant(String relationMainApplicant) {
        this.relationMainApplicant = relationMainApplicant;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public void setContactAddress(String contactAddress) {
        this.contactAddress = contactAddress;
    }

    public void setEnclosure(String enclosure) {
        this.enclosure = enclosure;
    }

    public void setSettleTime(Date settleTime) {
        this.settleTime = settleTime;
    }

    public void setSettleCountyTime(Date settleCountyTime) {
        this.settleCountyTime = settleCountyTime;
    }

    public void setWorkUnit(String workUnit) {
        this.workUnit = workUnit;
    }

    public void setIsReportDPI(Boolean isReportDPI) {
        this.isReportDPI = isReportDPI;
    }

    public void setIsReportProperty(Boolean isReportProperty) {
        this.isReportProperty = isReportProperty;
    }

    public void setIncomeDifference(Boolean incomeDifference) {
        this.incomeDifference = incomeDifference;
    }

    public void setIncomeDiffDes(String incomeDiffDes) {
        this.incomeDiffDes = incomeDiffDes;
    }

    public void setPropertyDifference(Boolean propertyDifference) {
        this.propertyDifference = propertyDifference;
    }

    public void setPropertyDiffDes(String propertyDiffDes) {
        this.propertyDiffDes = propertyDiffDes;
    }

    public void copyProperties(FamilyMember familyMember) {
        setBirthday(familyMember.getBirthday());
        setCountry(familyMember.getCountry());
        setDisabilityDegree(familyMember.getDisabilityDegree());
        setDiseaseName(familyMember.getDiseaseName());
        setEducation(familyMember.getEducation());
        setGender(familyMember.getGender());
        setHealthStatus(familyMember.getHealthStatus());
        setHouseholdAddress(familyMember.getHouseholdAddress());
        setHouseholdDistrictCode(familyMember.getHouseholdDistrictCode());
        setHouseholdType(familyMember.getHouseholdType());
        setIdNumber(familyMember.getIdNumber());
        setIdType(familyMember.getIdType());
        setLaborLossStatus(familyMember.getLaborLossStatus());
        setLivingAddress(familyMember.getLivingAddress());
        setLivingDistrictCode(familyMember.getLivingDistrictCode());
        setMainApplicant(familyMember.getMainApplicant());
        setMaritalStatus(familyMember.getMaritalStatus());
        setMarriedDate(familyMember.getMarriedDate());
        setName(familyMember.getName());
        setNation(familyMember.getNation());
        setPoliticalStatus(familyMember.getPoliticalStatus());
        setStartDateLiving(familyMember.getStartDateLiving());
        setPhone(familyMember.getPhone());
        setMobile(familyMember.getMobile());
        setZipCode(familyMember.getZipCode());
        setContactAddress(familyMember.getContactAddress());
    }

    @Transient
    public String getIdTypeAlias() {
        return DictionaryUtil.idTypeMap.get(idType);
    }

    @Transient
    public String getEducationAlias() {
        return DictionaryUtil.educationMap.get(education);
    }

    @Transient
    public String getMaritalStatusAlias() {
        return DictionaryUtil.marryMap.get(education);
    }

    private List<Enclosure> enclosureList;

    @Transient
    public List<Enclosure> getEnclosureList() {
        return enclosureList;
    }

    @Transient
    public void setEnclosureList(List<Enclosure> enclosureList) {
        this.enclosureList = enclosureList;
    }

    /**
     * 设置证明材料，以json串存储在enclosure里面
     */
    @Transient
    public void setEnclosures() {
        if (enclosureList != null && enclosureList.size() >= 0) {
            for (Enclosure ec : enclosureList)
                ec.setId(CommonUtil.getUUID());
            enclosure = JsonUtil.toJson(enclosureList);
        }
    }

    /**
     * 获取证明材料List
     */
    @Transient
    public List<Enclosure> getEnclosures() {
        List<Enclosure> list = Lists.newArrayList();
        if (StringUtils.isNotBlank(enclosure)) {
            list = JsonUtil.Json2List(enclosure, new TypeReference<List<Enclosure>>() {
            });
        }
        return list;
    }

}
